package com.zj.sixtyDayChallenge.array;

/**
 * @program: algorithm
 * @description: 长度最小的子数组
 * 209 minimum-size-subarray-sum
 * @author: Zhang Bo
 * @create: 2022-12-20 20:13:10
 **/
public class MinimumSizeSubarraySum {
    public static void main(String[] args) {
        Solution solution = new MinimumSizeSubarraySum().new Solution();
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        /**
         * [2,3,1,2,4,3]
         *
         * @param target
         * @param nums
         * @return
         */
        public int minSubArrayLen(int target, int[] nums) {
            int left = 0;
            int sum = 0;
            int result = Integer.MAX_VALUE;
            for (int right = 0; right < nums.length; right++) {
                sum += nums[right];
                while (sum >= target) {
                    result = Math.min(result, right - left + 1);
                    sum -= nums[left++];
                }
            }
            return result == Integer.MAX_VALUE ? 0 : result;

        }

        public int minSubArrayLen01(int target, int[] nums) {

            int i = 0;
            int sum = 0;
            int ret = Integer.MAX_VALUE;
            for (int j = 0; j < nums.length; j++) {
                sum += nums[j];
                while (sum >= target) {
                    ret = Math.min(j - i + 1, ret);
                    sum -= nums[i];
                    i++;
                }
            }

            return ret == Integer.MAX_VALUE ? 0 : ret;

        }

    }
//leetcode submit region end(Prohibit modification and deletion)

}

